<?php

namespace Shopadmin\Controller;
class CodeController extends AdminController
{
    public function code_list()
    {
        /*
         * 检测登录前的页面地址
         * */
        $shop_id = session("shopid");
        $search = I("search", "");
        $where['shop_id'] = $shop_id;
       // $where['status']=1;
        if (!empty($search)) {
            $where['title'] = array('like', "%$search%");
            $where['_logic'] = 'AND';
        }
        $count = M("qrcode")->where($where)->count();
        $Page = new \Think\Page($count, 10);
        $list = M("qrcode")->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->select();
        foreach ($list as &$v) {
            $v['codeimg'] = "/" . $v['codeimg'];
            $v['addtime'] = formarttime($v['addtime']);
            if($v['status']==0){
                $v['status']="待审核";
            }
            else{
                $v['status']="可用";
            }
        }
        if(empty($list)){
            $msg="你的二维码还没有通过管理员的审核,请耐心等待";

        }
        $shop_id=session("shopid");
        $info=M('shop')->where(array('id'=>$shop_id))->find();
        $this->assign("info", $info);
        $this->assign("pages", $Page->show());
        $this->assign("search", $search);
        $this->assign("list", $list);
        $this->assign("msg", $msg);
        $this->display();
    }

    public function add_code()
    {

        $shop_id = session("shopid");
        $shop = M('shop')->where(array('id' => $shop_id))->find();
        if (IS_POST) {
            $info = I("post.");
            $money = $info['money'];
            $quota_money=$info['quota_money'];
          if ($shop['money'] < $info['money']) {
                $shoppay_url = U("/shopadmin/Log/shop_pay/money/$money");
                $this->error("您好,您的账户余额不足以支付您设定的红包数额，请充值，将跳转到充值页面,请充值后再次尝试创建二维码", $shoppay_url, 5);
                exit;
            }
            $image = $this->upload();
            $image = $image['image'];
            $imagepath = "/uploads/" . $image['savepath'] . $image['savename'];
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
            $url = $http_type . $_SERVER['HTTP_HOST'];
            //二维码制定的id否则没有办法获得具体的红包数额
            $data = array(
                'shop_id' => session("shopid"),
                'title' => $info['title'],
                'image' => $imagepath,
                'content' => $info['content'],
                'money' => $info['money'],
                'quota_money'=>$quota_money,
                'max_money'=>$info['max_money'],
                'addtime' => time(),
                'sheng_money'=>$quota_money,
            );
            $re = M("qrcode")->add($data);
            $maxid=$re;
            $url .= U("/Home/Index/Index/id/$maxid");
            $qrimage = $this->create_image($url);
            $datas['codeimg']=$qrimage;
            $rs=M('qrcode')->where(array('id'=>$maxid))->save($datas);
            $success_url = U("/Shopadmin/Code/code_list");
            if ($re && $rs) {
                $this->success("添加成功,请等待管理员审核", $success_url);
            } else {
                $this->error("添加失败");
            }
        } else {
            $shop_id=session("shopid");
            $info=M('shop')->where(array('id'=>$shop_id))->find();
            $this->assign("info", $info);
            $this->display();
        }

    }

    public function update()
    {
        $shop_id = session("shopid");
        $id=I("get.id");
        $shop = M('shop')->where(array('id' => $shop_id))->find();
        if (IS_POST) {
            $info = I("post.");
            $money = $info['money'];
        if ($shop['money'] < $info['money']) {
                $shoppay_url = U("/shopadmin/Log/shop_pay/money/$money");
                $this->error("您好,您的账户余额不足以支付您设定的红包数额，请充值，将跳转到充值页面,请充值后再次尝试创建二维码", $shoppay_url, 5);
                exit;
        }
            $image = $this->upload();
            $image = $image['image'];
            $imagepath = "/uploads/" . $image['savepath'] . $image['savename'];
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
            $url = $http_type . $_SERVER['HTTP_HOST'];
            $url .= U("/Home/Index/Index/id/$id");
            $qrimage = $this->create_image($url);
            $data = array(
                'shop_id' => session("shopid"),
                'title' => $info['title'],
                'image' => $imagepath,
                'content' => $info['content'],
                'money' => $info['money'],
                'quota_money'=>$info['quota_money'],
                'addtime' => time(),
                'max_money'=>$info['max_money'],
                'sheng_money'=>$info['quota_money'],
                'codeimg' => $qrimage
            );
            $re = M("qrcode")->where(array('id' => $id))->save($data);
            if ($re) {
                $success_url = U("/Shopadmin/Code/code_list");
                if ($re) {
                    $this->success("修改成功", $success_url);
                } else {
                    $this->error("修改失败");
                }
            }
        } else {
            $shop_id=session("shopid");
            $shopinfo=M('shop')->where(array('id'=>$shop_id))->find();
            $this->assign("shopinfo", $shopinfo);
            $info = M("qrcode")->where(array('id' => $id))->find();
            $this->assign("info", $info);
            $this->display();
        }
    }
public function detail(){
    $id=I("get.id","");
    $shop_id = session("shopid");
    $hd_info=M('qrcode')->where(array('id'=>$id))->find();
    $search = I("search", "");
    $where['shop_id'] = $shop_id;
    $where['codeid'] = $id;
    $where['state']=1;
    if (!empty($search)) {
        $where['order_number'] = array('like', "%$search%");
        $where['_logic'] = 'AND';
    }
    $count = M("wx_transfer")->where($where)->count();
    echo M("wx_transfer")->where($where)->getLastSql();
    $Page = new \Think\Page($count, 10);
    $list = M("wx_transfer")->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->select();
    foreach ($list as &$v) {
        $user=M("wx_user")->where(array('openid'=>$v['openid']))->find();
        $v['addtime'] = timeString($v['addtime']);
        $v['nickname']=$user['nickname'];
        $v['sex']=$user['sex']==1?"男":"女";
        $v['city']=$user['city'];
        $v['headimgurl']=$user['headimgurl'];
        $v['money']= $v['money']/100;
        $v['count']= $user['count'];
        $v['scan_count']= $user['scan_count'];
    }
    $info=M('shop')->where(array('id'=>$shop_id))->find();
    $this->assign("info", $info);
    $this->assign("pages", $Page->show());
    $this->assign("search", $search);
    $this->assign("list", $list);
    $this->assign("hd_info", $hd_info);
    $this->display();
}
    public function delete()
    {
        $id = I("id", "");
        if (M("qrcode")->where("id=$id")->delete()) {
            $this->ajaxReturn(array("code" => 200, "msg" => "客户删除成功"));
        } else {
            $this->ajaxReturn(array("code" => 201, "msg" => "客户删除失败"));
        }
    }

    /**
     * 商家账户充值
     */
    public function shop_weixin_pay()
    {
        $shop_id = session("shopid");
        $shop_info = M('shop')->where(array('id' => $shop_id))->find();
        if (IS_POST) {
            $order_db = M('shop_recharge_order');
            $money = I('post.money', '', 'floatval');
            $order_number = date('YmdHis') . mt_rand('10000000', '99999999');
            $save_data['order_number'] = $order_number;
            $save_data['phone'] = $shop_info['phone'];
            $save_data['shop_id'] = $shop_id;
            $save_data['money'] = $money;  //费率的计算在支付成功之后修改用户余额的时候修改，这里不做修改
            $save_data['status'] = 0;   // 付款状态,0未付款 1 已付款
            $save_data['addtime'] = time();
            $save_data['pay_type'] = 2; //支付方式
            $success = $order_db->add($save_data);
            $chongzhi_notify_url = C('weixin_pay.chongzhi_notify_url');
            $rs = weixinpay($order_number, $money, '商家充值', 'NATIVE', $chongzhi_notify_url);
            if ($money && $success) {
                $this->ajaxReturn(array(
                    'code' => 200,
                    'codeurl' => "http://qr.liantu.com/api.php?w=240&text=" . $rs['code_url'],
                    'order_number' => $order_number
                ));
            } else {
                $res = array('code' => 201, 'res' => false, 'msg' => $rs);
                $this->ajaxReturn($res);
            }
        } else {
            $shop_id = session("shopid");
            $this->assign("shop_id", $shop_id);
            $this->display();
        }

    }
    public function shop_alipay_pay(){
        $order_db = M('shop_recharge_order');
        $shop_id=session("shopid");
        $pay_type=I("get.pay_type","");
        $money=I("get.money",0);
        $shop_info = M('shop')->where(array('id' => $shop_id))->find();
        $order_number = date('YmdHis') . mt_rand('10000000', '99999999');
        $save_data['order_number'] = $order_number;
        $save_data['phone'] = $shop_info['phone'];
        $save_data['shop_id'] = $shop_id;
        $save_data['money'] = $money;  //费率的计算在支付成功之后修改用户余额的时候修改，这里不做修改
        $save_data['status'] = 0;   // 付款状态,0未付款 1 已付款
        $save_data['addtime'] = time();
        $save_data['pay_type'] = $pay_type; //支付方式
        $success = $order_db->add($save_data);
        $alipay_chongzhi_notify_url = C('alipay_conf.notify_url');
        $alipay_chongzhi_return_url = C('alipay_conf.return_url');
        $re= alipay($order_number,"商家充值",$money,"",$alipay_chongzhi_notify_url,$alipay_chongzhi_return_url);
        if($success && $re){
            echo $re;
        }
        else{
            echo "fail";
        }
    }
    function getPayStatus()
    {
        $order_number=I("post.order_number","");

        $order=M("shop_recharge_order")->where(array('order_number'=>$order_number))->find();
        $succurl=U("/Shopadmin/Login/info");
        if($order['status']==1)
        {
            $this->ajaxReturn(
                array(
                 'code'=>200,
                 'url'=>$succurl,
                 'msg'=>"支付成功"
                )
            );
        }

        else{
            $this->ajaxReturn(
                array(
                    'code'=>201,
                    'msg'=>"支付失败"
                )
            );
        }

    }

}